// pages/finance-order/finance-order.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ["0", "1"],
    startDate: "",
    endDate: "",
    params: {
      bgeinDate: "",
      endDate: "",
      supplierId: ""
    },
    result: [],
    orderList: [],
    currentDate: new Date().getTime(),
    total: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const {
      supplierId,
      filterActive,
      supplierName
    } = JSON.parse(options.data)
    const date = this.getDate()
    this.setData({
      filterActive: filterActive,
      activeTag: supplierId,
      activeTagName: supplierName,
      'params.supplierId': supplierId,
      'params.isPayment': filterActive,
    }, () => {
      this.getFinance2()
      this.getSupplierList(filterActive)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getFinance()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  showStart() {
    this.setData({
      startShow: true
    })
  },
  showEnd() {
    this.setData({
      endShow: true
    })
  },
  showPer(){
    this.setData({
      perShow: true
    })
  },
  onClose() {
    this.setData({
      startShow: false,
      endShow: false,
      perShow: false
    })

  },
  startConfirm(e) {
    this.setData({
      'params.bgeinDate': this.formatDate(e.detail),
      startShow: false
    }, () => {
      this.getFinance()
    })
  },
  endConfirm(e) {
    this.setData({
      'params.endDate': this.formatDate(e.detail),
      endShow: false
    }, () => {
      this.getFinance()
    })
  },
  formatDate(date) {
    date = new Date(date)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return year + '-' + month + '-' + day
  },

  getDate() {
    var nowDate = new Date();
    var cloneNowDate = new Date();
    var fullYear = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1; //
    var endOfMonth = new Date(fullYear, month, 0).getDate(); // 获取本月最后一天
    function getFullDate(targetDate) {
      var D, y, m, d;
      if (targetDate) {
        D = new Date(targetDate);
        y = D.getFullYear();
        m = D.getMonth() + 1;
        d = D.getDate();
      } else {
        y = fullYear;
        m = month;
        d = date;
      }
      m = m > 9 ? m : '0' + m;
      d = d > 9 ? d : '0' + d;
      return y + '-' + m + '-' + d;
    };
    var endDate = getFullDate(cloneNowDate.setDate(endOfMonth)); //当月最后一天
    var starDate = getFullDate(cloneNowDate.setDate(1)); //当月第一天
    return [starDate, endDate]
  },

  changeCollapse(event) {
    console.log(event.detail)
    this.setData({
      activeNames: event.detail
    });
  },

  getSupplierList(type) {
    app.reqFetch.selectSupplierFinance({
      isPayment: type
    }, (res) => {
      this.setData({
        supplierList: res.data,
      })
    })
  },
  onPerConfirm(e) {
    console.log(e)
    const {
      supplierId,
      supplierName
    } = e.detail.value
    this.setData({
      activeTag: supplierId,
      activeTagName: supplierName,
      'params.supplierId': supplierId,
      'params.bgeinDate': '',
      'params.endDate': '',
      perShow: false
    }, () => this.getFinance2())
  },


  getFinance() {
    app.reqFetch.selectFinance({ ...this.data.params
    }, (res) => {
      const {
        financemxList = [], ntotalNum, ytotalNum, firstDate, lastDate
      } = res.data
      this.setData({
        result: [],
        ntotalNum,
        ytotalNum,
        orderList: financemxList
      })
    })
  },
  getFinance2() {
    app.reqFetch.selectFinance({
      ...this.data.params
    }, (res) => {
      const {
        financemxList = [], ntotalNum, ytotalNum, firstDate, lastDate
      } = res.data
      this.setData({
        result: [],
        'params.bgeinDate': firstDate.substring(0, 10),
        'params.endDate': lastDate.substring(0, 10),
        ntotalNum,
        ytotalNum,
        orderList: financemxList
      })
    })
  },
  onChange(event) {
    this.setData({
      result: event.detail
    }, () => this.getTotal());
  },
  getTotal() {
    const {
      orderList,
      result
    } = this.data
    let total = 0
    result.map(item => {
      total += orderList.filter(ele => item === ele.financeId)[0].amountPayable
    })
    this.setData({
      total
    })

  },
  toDetails(e) {
    const {
      supplierId,
      orderid,
      type,
      auditTime,
      createDate,
      financeId,
      isPayment,
      chedName,
      canteenAdminName,
      amountPayable
    } = e.currentTarget.dataset.record

    wx.navigateTo({
      url: '/pages/finance-order-details/finance-order-details?data=' + JSON.stringify({
        chedName,
        canteenAdminName,
        amountPayable,
        isPayment,
        supplierId,
        orderid,
        type,
        auditTime,
        createDate,
        financeId
      }),
    })
  },
  handleConfirm() {
    const {
      orderList,
      result
    } = this.data
    app.reqFetch.financeAdminUpdate({
      financeIds: result.join(','),
      isPayment: 1,
    }, (res) => {
      this.getFinance()
    })
  },
  handleConfirm() {
    const {
      orderList,
      result
    } = this.data
    app.reqFetch.financeAdminUpdate({
      financeIds: result.join(','),
      isPayment: 2,
    }, (res) => {
      this.getFinance()
    })
  },
  checkAll(e) {
    const {
      orderList
    } = this.data
    this.setData({
      checkAll: e.detail
    })
    if (e.detail) {
      let result = []
      orderList.map(item => {
        result.push(item.financeId)
      })
      this.setData({
        result
      }, () => this.getTotal())
    } else {
      this.setData({
        result: []
      }, () => this.getTotal())
    }


  }
})